//
//  upDeviceToken.m
//  SoundPill
//
//  Created by 宋旭东 on 15/7/30.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "upDeviceToken.h"

@implementation upDeviceToken
- (instancetype)initWithDeviceToken:(NSString *)deviceToken Uid:(NSString *)uid
{
    self = [super init];
    if (self) {
        if (deviceToken && uid) {
            [self upDeviceToken:deviceToken uid:uid];
        } else {
            [self upDeviceToken:deviceToken];
        }
    }
    return self;
}
- (void)upDeviceToken:(NSString *)deviceToken uid:(NSString *)uid
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:deviceToken, @"deviceToken", uid, @"uid", nil];
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://123.57.1.143/api/regdevice" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSFileManager *manager = [NSFileManager defaultManager];
        NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDirectory = [directoryPaths objectAtIndex:0];
        NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"DeviceToken"];
        NSString *oldDeviceToken = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        if (![manager fileExistsAtPath:filePath] ||![deviceToken isEqualToString:oldDeviceToken]) {
            [manager createFileAtPath:filePath contents:nil attributes:nil];
            [deviceToken writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (void)upDeviceToken:(NSString *)deviceToken
{
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:deviceToken, @"deviceToken", nil];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:@"http://123.57.1.143/api/regdevice" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSFileManager *manager = [NSFileManager defaultManager];
        NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentDirectory = [directoryPaths objectAtIndex:0];
        NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"DeviceToken"];
        NSString *oldDeviceToken = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        if (![manager fileExistsAtPath:filePath] ||![deviceToken isEqualToString:oldDeviceToken]) {
            [manager createFileAtPath:filePath contents:nil attributes:nil];
            [deviceToken writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
@end
